package cn.bdqn.hospital.dao.impl;

import cn.bdqn.hospital.dao.CfDao;
import cn.bdqn.hospital.entity.Cf;
import cn.bdqn.hospital.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 处方刀实现类
 */
public class CfDaoImpl implements CfDao {

    QueryRunner queryRunner = new QueryRunner();

    /**
     * 查询所有处方
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<Cf> getAll() throws SQLException {
        List<Cf> cfList = new ArrayList<Cf>();
        try{
            String sql = "select * from Cf;";
            cfList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Cf>(Cf.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return cfList;
    }

    /**
     * 通过处方id查询信息
     *
     * @param cfId
     * @return
     * @throws SQLException
     */
    @Override
    public Cf getCfByCfid(Integer cfId) throws SQLException {
        Cf cf = null;
        try{
            String sql = "select * from Cf where cfId = '?'";
            cf = (Cf) queryRunner.query(sql,new BeanListHandler<Cf>(Cf.class),cfId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cf;
    }

    /**
     * 添加处方信息
     *
     * @param cf
     * @return
     * @throws SQLException
     */
    @Override
    public int add(Cf cf) throws SQLException {
        String sql = "insert into Cf(ypId, cfMessage, ddId, cfTime, orderId) values (?,?,?,?,?);";
        Object[] Cf = new Object[]{cf.getYpId(),cf.getCfMessage(),cf.getDdId(),cf.getCfTime(),cf.getOrderId()};
        return queryRunner.update(DruidUtils.getConnection(), sql, cf);
    }

    /**
     * 修改处方信息
     *
     * @param cf
     * @return
     * @throws SQLException
     */
    @Override
    public int update(Cf cf) throws SQLException {
        int num = 0;
        String sql = "update Cf set cfMessage = ? where cfId = ?;";
        num = queryRunner.update(DruidUtils.getConnection(),sql,cf);
        return num;
    }

    /**
     * 删除处方信息
     *
     * @param cfId
     * @return
     * @throws SQLException
     */
    @Override
    public int delCfByCfid(Integer cfId) throws SQLException {
        int num = 0;
        try{
            String sql = "delete from Cf where cfId='?';";
            num = queryRunner.update(DruidUtils.getConnection(),sql,cfId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 通过缴费号查询
     *
     * @param ddId
     * @return
     * @throws SQLException
     */
    @Override
    public Cf getCfByDdid(Integer ddId) throws SQLException {
        Cf cf = null;
        try{
            String sql = "select * from Cf where ddId = '?'";
            cf = (Cf) queryRunner.query(sql,new BeanListHandler<Cf>(Cf.class),ddId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cf;
    }

    /**
     * 通过预约号查询
     *
     * @param orderId
     * @return
     * @throws SQLException
     */
    @Override
    public Cf getCfByOrderid(Integer orderId) throws SQLException {
        Cf cf = null;
        try{
            String sql = "select * from Cf where orderId = '?'";
            cf = (Cf) queryRunner.query(sql,new BeanListHandler<Cf>(Cf.class),orderId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cf;
    }
}
